import os,sys,json
from flask import Flask, request, Response, make_response
from flask import jsonify

import base64
import numpy as np
import cv2
import time
import datetime

app = Flask(__name__)


@app.route('/hello')
def hello():
    return 'Hello, Flask!'


@app.route('/post', methods=['POST', 'GET', 'DELETE'])
def post():
    if request.method != 'POST':
        pass
    else:
        print(request.get_data())
        data = json.loads(request.get_data().decode('utf-8'))

        #keys
        keys = str(data['keys'])
        print(keys)

        #base64
        imagedata = base64.b64decode(data['base64'])
        nparr = np.fromstring(imagedata,dtype=np.uint8)
        image = cv2.imdecode(nparr,cv2.IMREAD_COLOR)

        #save out
        imname = 'receive.jpg'
        cv2.imwrite(imname, image);

    resp = make_response(jsonify('done'))
    resp.headers['Content-type'] = 'application/json;charset=utf-8'
    return resp


if __name__ == '__main__':
    app.run(host='0.0.0.0',port=10088)

